package  
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.system.Security;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.external.ExternalInterface;
	
	import com.app.model.web.Js;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class Starting extends Sprite
	{
		private static var _instance:Starting = null;
		
		private var _loader:Loader = null;
		
		private var _webs:Object = null;
		
		private var _animation:MovieClip = null;
		
		public function Starting() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage, false, 0, true);
		}
		
		private function onAddToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			
			_instance = this;
			
			_animation = this["mc_animation_view"];
			
			Security.allowDomain( "*");
			Security.allowInsecureDomain("*");
			
			if (!jsCall()) return;
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.load(new URLRequest(getSwfUrl("loading")));
		}
		
		private function onComplete(event:Event):void
		{
			this.addChild(_loader);
		}
		
		private function jsCall():Boolean
		{
			if (ExternalInterface.available)
			{
				_webs = Js.call(Js.GET_FLASH_VARS);
				if (_webs == null)
				{
					throw("webs is empty!");
					return false;
				}
			}
			
			return true;
		}
		
		public function getSwfUrl(swfName:String):String
		{	
			var swf:String = "";
			if (typeof webs["vers"] != "undefined")
			{
				swf = webs["vers"][swfName];
			}
			
			var url:String = webs["flash_path"];
			return 	(url + swf);
			
		}
		
		public function remove():void
		{
			if (_animation) {
				_animation.stop();
				_animation.visible = false;
			}
		}
		
		public function get webs():Object { return _webs; }
		
		public static function get instance():Starting { return _instance; }
	}

}